$NG.AllReady(function (page, { useAction, useBeforeClick, useValuesChange, useClick, useUpdateRow, subscribe, useUpdateRows }) {
    const mstform = $NG.getCmpApi("p_form_fbjs_m")
    const dgrid1 = $NG.getCmpApi("p_form_fbjs_d1")
    const getHeadValue = (str) => mstform.getItem(str).getValue()

    let config = {}
    getTotalAmt()
    if (page.oType == 'add') {
        setDateNum()
        mstform.getItem("u_jsnd").setValue({
            label: new Date().getFullYear(),
            value: new Date().getFullYear()
        })
    }
    setHiddenTabs()
    const amtkeys = ["u_bqzbj", "u_bqaqbzj", "u_bqmgqybzj", "u_bqlybzj", "u_bqqtbzj", "u_bqhj", "u_bqjsk",
        "u_klzbj", "u_klaqbzj", "u_klmgqybzj", "u_kllybzj", "u_klqtbzj", "u_klhj", "u_kljsk",]
    const rateKeys = ["u_zbj", "u_aqbzj", "u_mgqybzj", "u_lybzj", "u_qtbzj",]
    const temp = [...amtkeys, ...rateKeys]
    temp.forEach(el => {
        useValuesChange(() => {
            SumHeadAmt()
        }, "p_form_fbjs_m." + el)
    })
    useValuesChange(() => {
        setDateNum()
        getTotalAmt()
        getCntCbInfoByPhid()
    }, "p_form_fbjs_m.u_fbhtmc")

    useValuesChange(() => {
        console.log(`u_mxxz`,);
        setHiddenTabs()
    }, "p_form_fbjs_m.u_mxxz")

    useUpdateRow(({ args }) => {
        console.log(`编辑17`,);
        const item = args[0]
        let it = {}
        // rows.forEach(item=>{
        // const itconfig = config.filter((el)=>{
        //     if(el.s_tree_no == item.s_tree_no){
        //         return el
        //     }
        // })
        // if(itconfig.length == 0){
        //     return 
        // }
        it = config[item.s_tree_no] || {}
        item.u_gcl1 = (Number(it.total_gcl) || 0)
        item.u_hj1 = (Number(it.total_hj) || 0)
        item.u_gcl3 = (Number(it.total_gcl) || 0) + (Number(item.u_gcl2) || 0)
        item.u_hj3 = (Number(it.total_hj) || 0) + (Number(item.u_hj2) || 0)
        // })
        $NG.getCmpApi("p_form_fbjs_d18").refreshView()
    }, "p_form_fbjs_d18")

    // 计算表头价格
    function SumHeadAmt() {
        let config = {}
        amtkeys.forEach(el => {
            config[el] = getHeadValue(el)
        })
        rateKeys.forEach(el => {
            config[el] = (Number(mstform.getItem(el).getText()?.replaceAll("%", "")) || 0) / 100
        })
        console.log(`config`, { ...config });
        debugger
        config.u_bqzbj = (Number(config.u_zbj) || 0) * (Number(config.u_bqjsk) || 0)
        config.u_bqaqbzj = (Number(config.u_aqbzj) || 0) * (Number(config.u_bqjsk) || 0)
        config.u_bqmgqybzj = (Number(config.u_mgqybzj) || 0) * (Number(config.u_bqjsk) || 0)
        config.u_bqlybzj = (Number(config.u_lybzj) || 0) * (Number(config.u_bqjsk) || 0)
        config.u_bqqtbzj = (Number(config.u_qtbzj) || 0) * (Number(config.u_bqjsk) || 0)
        config.u_klzbj = (Number(config.u_zbj) || 0) * (Number(config.u_kljsk) || 0)
        config.u_klaqbzj = (Number(config.u_aqbzj) || 0) * (Number(config.u_kljsk) || 0)
        config.u_klmgqybzj = (Number(config.u_mgqybzj) || 0) * (Number(config.u_kljsk) || 0)
        config.u_kllybzj = (Number(config.u_lybzj) || 0) * (Number(config.u_kljsk) || 0)
        config.u_klqtbzj = (Number(config.u_qtbzj) || 0) * (Number(config.u_kljsk) || 0)
        rateKeys.forEach(item => {
            delete config[item]
        })
        console.log(`config_sss`, { ...config });
        $NG.updateState((updater) => {
            updater.data.p_form_fbjs_m.setProps({ ...config })
        })
    }

    // function 找期数
    async function setDateNum() {
        const res = await $NG.execServer("SELECT", { sql: `select phid from p_form_fbjs_m` })
        mstform.getItem("u_zqs").setValue(res.count + 1)
        const phid = getHeadValue("u_fbhtmc")
        if (!phid) return
        const res1 = await $NG.execServer("SELECT", { sql: `select phid from p_form_fbjs_m where u_fbhtmc = '${phid}'` })
        mstform.getItem("u_jsqs").setValue(res1.count + 1)
    }

    function getTotalAmt() {
        const phid = mstform.getItem("u_fbhtmc").getValue()
        const mphid = mstform.getValues().phid || "0"
        if (!phid) return
        $NG.execServer("SELECT", { sql: `select d.s_tree_no,sum(d.u_gcl2) as total_gcl,sum(d.u_hj2) as total_hj from p_form_fbjs_m m left join p_form_fbjs_d18 d on m.phid = d.pphid where m.u_fbhtmc = '${phid}' and m.phid != '${mphid}' group by d.s_tree_no` }, (res) => {
            if (res.count == 0) return
            const datas = JSON.parse(res.data).reduce((value,item)=>{
                const keys = Object.keys(value)
                if(keys.includes(item.extendObjects.s_tree_no)){
                    value[item.extendObjects.s_tree_no] = {
                        total_gcl: value[item.extendObjects.s_tree_no] += (Number(total_gcl) || 0),
                        total_hj: value[item.extendObjects.s_tree_no] += (Number(total_hj) || 0)
                    }
                } else {
                    value[item.extendObjects.s_tree_no] = {
                        total_gcl: item.extendObjects.total_gcl,
                        total_hj: item.extendObjects.total_hj
                    }
                }
                return value
            },{})
            config = { ...datas }
            window._temp_config = config
        })
    }

    function getCntCbInfoByPhid() {
        const phid = mstform.getItem("u_fbhtmc").getValue()
        if (!phid) return
        $NG.execServer("getCntCbInfoByPhid", { phid }, (res) => {
            if (res.count == 0) return
            const data = JSON.parse(res.data)[0].extendObjects
            const arr = [
                {
                    key: "bill_no",
                    value: data.cnt_no,
                },
                {
                    key: "phid_pc",
                    value: {
                        label: data.phid_pc_exname,
                        value: data.phid_pc
                    }
                },
                {
                    key: "u_fbsmc",
                    value: {
                        label: data.jfdw_name,
                        value: data.jfdw_phid
                    }
                },
                {
                    key: "u_fbhtje",
                    value: data.user_fbhtje
                }
            ]
            setHeadValues(arr)
        })
    }

    function setHiddenTabs() {
        const tabs = $NG
            .getCompMap()
            .get("__Tabs__")[0]
            .getApi();
        tabs.updateItems((innerItems, activeKey) => {
            return [...innerItems.filter((item) => {
                switch ((Number(getHeadValue("u_mxxz")) || 0)) {
                    case 1:
                        console.log(`1111`,);
                        if (item.name == 'tabPanel_tabItem' || item.name == 'tabPanel_tabItem_1') {
                            return item
                        }

                        break
                    case 2:
                        if (['tabPanel_tabItem_2', 'tabPanel_tabItem_3', 'tabPanel_tabItem_4', 'tabPanel_tabItem_5',].includes(item.name)) {
                            return item
                        }
                        break
                    case 3:
                        if ([
                            'tabPanel_tabItem_7',
                            'tabPanel_tabItem_8',
                            'tabPanel_tabItem_9',
                            'tabPanel_tabItem_10',
                            'tabPanel_tabItem_11',
                            'tabPanel_tabItem_12',
                            'tabPanel_tabItem_13',

                        ].includes(item.name)) {
                            return item
                        }
                        break
                    case 4:
                        if ([
                            'tabPanel_tabItem_16',

                        ].includes(item.name)) {
                            return item
                        }
                        break
                    case 5:
                        if ([
                            'tabPanel_tabItem_14',

                        ].includes(item.name)) {
                            return item
                        }
                        break
                    case 6:
                        if ([
                            'tabPanel_tabItem_17',

                        ].includes(item.name)) {
                            return item
                        }
                        break
                    default:
                }
            })];
        });

        switch ((Number(getHeadValue("u_mxxz")) || 0)) {
            case 1:
                tabs.setActiveKey("tabPanel_tabItem");
                break
            case 2:
                tabs.setActiveKey("tabPanel_tabItem_2");
                break
            case 3:
                tabs.setActiveKey("tabPanel_tabItem_7");
                break
            case 4:
                tabs.setActiveKey("tabPanel_tabItem_16");
                break
            case 5:
                tabs.setActiveKey("tabPanel_tabItem_14");
                break
            case 6:
                tabs.setActiveKey("tabPanel_tabItem_17");

                break
            default:
        }

    }

    // 表头批量赋值
    function setHeadValues(arr) {
        arr.forEach(item => {
            console.log(`执行更新`, item);
            mstform.getItem(item.key)?.setValue(item.value)
        })
    }
})